package com.mirror.controller;

import com.mirror.entity.building.Bed;
import com.mirror.entity.building.Room;
import com.mirror.entity.oldman.Result;
import com.mirror.repository.BedRepository;
import com.mirror.repository.FloorRepository;
import com.mirror.repository.RoomRepository;
import com.mirror.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Api(description = "床位管理")
@RestController
@RequestMapping("bed")
public class BedController {


    @Autowired
    private BedRepository bedRepository;
    @Autowired
    private FloorRepository floorRepository;
    @Autowired
    private RoomRepository roomRepository;


    @ApiOperation(value = "床位列表无分页", notes = "床位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"roomId\": \"房间id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/list")
    public Result list(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String roomId = tempData.getString("roomId");
        Room b = roomRepository.findOne(roomId);
        if (b == null) {
            return ResultUtil.error(-1, "获取房间失败");
        }
        List<Bed> blist = bedRepository.findAllByIsdelAndRoomOrderByBedName(false, b);
        return ResultUtil.success(blist);
    }

    @ApiOperation(value = "床位列表", notes = "床位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"page\": \"页码\",\"size\": \"每页数量\",\"roomId\": \"房间id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/pagelist")
    public Result atList(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        int page = tempData.getInt("page");
        int size = tempData.getInt("size");
        String roomId = tempData.getString("roomId");
        Room b = roomRepository.findOne(roomId);
        if (b == null) {
            return ResultUtil.error(-1, "获取房间失败");
        }
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<Bed> atPage = bedRepository.findAllByIsdelAndRoomOrderByBedName(pageable, false, b);
        return ResultUtil.success(atPage);
    }


    @ApiOperation(value = "添加床位", notes = "添加床位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"roomId\": \"房间id\",\"bedName\": \"床位名\",\"memo\": \"备注\"}", required = true, dataType = "String"),
    })
    @PostMapping("/save")
    public Result save(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String roomId = tempData.getString("roomId");
        String memo = tempData.optString("memo");
        String bedName = tempData.getString("bedName");
        Bed a = new Bed();
        a.setCreateDate(new Date());
        Room b = roomRepository.findOne(roomId);
        if (b == null) {
            return ResultUtil.error(-1, "获取房间失败");
        }
        a.setRoom(b);
        a.setMemo(memo);
        a.setBedName(bedName);
        a.setHasPeople(false);
        a.setIsdel(false);
        b.setBedsNum(b.getBedsNum() + 1);
        roomRepository.save(b);
        return ResultUtil.success(bedRepository.save(a));
    }

    @ApiOperation(value = "修改床位", notes = "修改床位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\": \"床位id\",\"bedName\": \"床位名\",\"memo\": \"备注\"}", required = true, dataType = "String"),
    })
    @PostMapping("/edit")
    public Result edit(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String bedId = tempData.getString("id");
        String memo = tempData.optString("memo");
        String bedName = tempData.getString("bedName");
        Bed a = bedRepository.findOne(bedId);
        if (a == null) {
            return ResultUtil.error(-1, "获取床位失败！");
        }
        a.setMemo(memo);
        a.setBedName(bedName);
        return ResultUtil.success(bedRepository.save(a));
    }

    @ApiOperation(value = "删除床位", notes = "删除床位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\": \"床位id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/del")
    public Result del(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String bedId = tempData.getString("id");
        Bed a = bedRepository.findOne(bedId);
        if (a == null) {
            return ResultUtil.error(-1, "获取床位失败！");
        }
        a.setIsdel(true);
        a.getRoom().setBedsNum(a.getRoom().getBedsNum() - 1);
        roomRepository.save(a.getRoom());
        return ResultUtil.success(bedRepository.save(a));
    }

}

